﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using MingleReport.HttpRequests;
using MingleReport.Services;

namespace MingleReport
{
    public partial class SearchPropertiesForm : Form
    {
        private readonly IMingleSession application;
        private readonly IStoryCardRepository storyCardRepository;

        public SearchPropertiesForm(IMingleSession session, string cardType)
        {
            application = session;
            InitializeComponent();
            storyCardRepository = new StoryCardRepository(session, new HttpRequestFactory(), 
                new XmlDocumentParser<StoryCard>());
            initSearch(cardType);
        }

        private void initSearch(string cardType)
        {
            List<string> cardTypesDisplayList = storyCardRepository.GetAllCardPropertiesByCardType(cardType);
            listPropertyName.DataSource = cardTypesDisplayList;
        }


        private void btnExportAllProperties_Click(object sender, EventArgs e)
        {
            /**
            List<string> propertyNames = new List<string>();
            for (int i = 0; i < listPropertyName.Items.Count; i++)
            {
                propertyNames.Add(listPropertyName.Items[i].ToString());
            }

            //new MingleExcel(application).Export(propertyNames);
            Close();
             * */
            // TODO replace below functionality with filtered results
            List<StoryCard> cards = storyCardRepository.GetAllStoryCardsByProjectId();
            new MingleExcel(application).Export(cards);
            if (Application.OpenForms["MingleReportingForm"] != null)
            {
                Application.OpenForms["MingleReportingForm"].Close(); 
            }
            Close();
        }

        private void btnExportSelectedProperty_Click(object sender, EventArgs e)
        {
            // TODO replace below functionality with filtered results
            List<StoryCard> cards = storyCardRepository.GetAllStoryCardsByProjectId();
            new MingleExcel(application).Export(cards);
            if (Application.OpenForms["MingleReportingForm"] != null)
            {
                Application.OpenForms["MingleReportingForm"].Close();
            }
            Close();
        }

    }
}